<?php 
/**
 * 
 * uses protected sfImageCropGD method for photos croping
 * @author Jieva
 *
 */
class ACrop extends sfImageCropGD
{
    public function transform(sfImage $image)
    {
        $cr = new sfImageCropGD($this->left, $this->top, $this->width, $this->height);
        $imagenew = $cr->transform($image);
        return $imagenew;
    }
    public static function reduceToWindowSize($windowHeight, $windowWidth, $imageHeight, $imageWidth, $file, $fileType)
    {
        if(($imageWidth>$windowWidth)||($imageHeight>$windowHeight))
        {
            $img = new sfImage($file, $fileType);
            //Resize works when width or height is null
            if($imageWidth>$windowWidth)
            {
                $res = $img->resize($windowWidth, 0);
            }elseif($imageHeight>$windowHeight)
            {
                $res = $img->resize(0, $windowHeight);
            }
            if( $res )
            {
                $img->save();
            }
        }
    }
}

/**
 * 
 * Class to save temporary info in user session by croping photos.
 * @author Jieva
 *
 */
class TempFileInfo
{
    protected $objType = '';
    protected $objId = '';
    protected $state = 1;
    protected $tempDir = 'temporary';
    protected $fileName = '';
    protected $dirToSave = '';
    protected $fileType = '';
    protected $fileExtension = '';
    protected $filei18nName = '';
    protected $fileText = '';
    protected $arDirectories = array();
    protected $returnUrl = '';
    
    /**
     * @return the $filei18nName
     */
    public function getFilei18nName ()
    {
        return $this->filei18nName;
    }
    /**
     * 
     * string returnUrl, returns to same module after cropping
     */
    public function getReturnUrl()
    {
        return $this->returnUrl;
    }

	/**
     * @return the $fileText
     */
    public function getFileText ()
    {
        return $this->fileText;
    }

	/**
     * @param field_type $filei18nName
     */
    public function setFilei18nName ($filei18nName)
    {
        $this->filei18nName = $filei18nName;
    }

	/**
     * @param field_type $fileText
     */
    public function setFileText ($fileText)
    {
        $this->fileText = $fileText;
    }

	public function getCountDirs()
    {
        if(empty($this->arDirectories))
        {
            $this->arDirectories = Files::getPhotoDirs();
        }
        return count($this->arDirectories);
    }
    public function getDirByCurrentState()
    {
        if(empty($this->arDirectories))
        {
            $this->arDirectories = Files::getPhotoDirs();
        }
        if($this->state < 1)
        {
            throw new Exception('State less than 1.');
        }
        if($this->state > $this->getCountDirs())
        {
            throw new Exception('State to high.');
        }
        $st = 1;
        foreach ( $this->arDirectories as $photoAr )
        {
            if( $st == $this->state )
            {
                return $photoAr['dir'];
            }
            $st++;
        }
    }
    
    /**
     * @return the $objType
     */
    public function getObjType ()
    {
        return $this->objType;
    }
    public function getHeightByState()
    {
        $name = self::getDirByCurrentState();
        $ar = $this->arDirectories;
        if(isset($ar[$name]['h']))
        {
            return $ar[$name]['h']; 
        }else 
        {
            return 0;
        }
    }
    public function getWidthByState()
    {
        $name = self::getDirByCurrentState();
        $ar = $this->arDirectories;
        if(isset($ar[$name]['w']))
        {
            return $ar[$name]['w']; 
        }else 
        {
            return 0;
        }
    }
	/**
     * @return the $objId
     */
    public function getObjId ()
    {
        return $this->objId;
    }

	/**
     * @return the $state
     */
    public function getState ()
    {
        return $this->state;
    }
	/**
     * @param field_type $objType
     */
    public function setObjType ($objType)
    {
        $this->objType = $objType;
    }

	/**
     * @param field_type $objId
     */
    public function setObjId ($objId)
    {
        $this->objId = $objId;
    }

	/**
     * @param field_type $state
     */
    public function setState ($state)
    {
        $this->state = $state;
    }

	/**
     * @return the $arDirectories
     */
    public function getArDirectories ()
    {
        return $this->arDirectories;
    }

	/**
     * @param field_type $arDirectories
     */
    public function setArDirectories ($arDirectories)
    {
        $this->arDirectories = $arDirectories;
    }

	/**
     * @return the $fileType
     */
    public function getFileType ()
    {
        return $this->fileType;
    }

	/**
     * @return the $fileExtension
     */
    public function getFileExtension ()
    {
        return $this->fileExtension;
    }

	/**
     * @param field_type $fileType
     */
    public function setFileType ($fileType)
    {
        $this->fileType = $fileType;
    }

	/**
     * @param field_type $fileExtension
     */
    public function setFileExtension ($fileExtension)
    {
        $this->fileExtension = $fileExtension;
    }

	/**
     * @return the $tempDir
     */
    public function getTempDir ()
    {
        return $this->tempDir;
    }

	/**
     * @return the $fileName
     */
    public function getFileName ()
    {
        return $this->fileName;
    }

	/**
     * @return the $dirToSave
     */
    public function getDirToSave ()
    {
        return $this->dirToSave;
    }

	/**
     * @param field_type $tempDir
     */
    public function setTempDir ($tempDir)
    {
        $this->tempDir = $tempDir;
    }

	/**
     * @param field_type $fileName
     */
    public function setFileName ($fileName)
    {
        $this->fileName = $fileName;
    }

	/**
     * @param field_type $dirToSave
     */
    public function setDirToSave ($dirToSave)
    {
        $this->dirToSave = $dirToSave;
    }
    /**
     * 
     * url relative
     * @param string $url - relativa
     */
    public function setReturnUrl($url)
    {
        $this->returnUrl = $url;
    }

    
}